package compare

import (
	"github.com/duke-git/lancet/v2/convertor"
	"github.com/duke-git/lancet/v2/mathutil"
	"golang.org/x/exp/constraints"
	"reflect"
	"time"
)

// 常量
const (
	equal          = "eq"
	lessThan       = "lt"
	greaterThan    = "gt"
	lessOrEqual    = "le"
	greaterOrEqual = "ge"
)

// 常量变量
var (
	timeType  = reflect.TypeOf(time.Time{})
	bytesType = reflect.TypeOf([]byte{})
)

// Equal 比较器相等
// @param left 左值
// @param right 右值
// @return bool
func Equal(left, right any) bool {
	return compareValue(equal, left, right)
}

// EqualValue 比较器相等值
// @param left 左值
// @param right 右值
// @return bool
func EqualValue(left, right any) bool {
	local, rival := convertor.ToString(left), convertor.ToString(right)
	return local == rival
}

// LessThan 比较器小于
// @param left 左值
// @param right 右值
// @return bool
func LessThan(left, right any) bool {
	return compareValue(lessThan, left, right)
}

// GreaterThan 比较器大于
// @param left 左值
// @param right 右值
// @return bool
func GreaterThan(left, right any) bool {
	return compareValue(greaterThan, left, right)
}

// LessOrEqual 比较器小于等于
// @param left 左值
// @param right 右值
// @return bool
func LessOrEqual(left, right any) bool {
	return compareValue(lessOrEqual, left, right)
}

// GreaterOrEqual 比较器大于等于
// @param left 左值
// @param right 右值
// @return bool
func GreaterOrEqual(left, right any) bool {
	return compareValue(greaterOrEqual, left, right)
}

// InDelta 比较器差值在delta内
// @param left 左值
// @param right 右值
// @param delta 差值
// @return bool
func InDelta[T constraints.Integer | constraints.Float](left, right T, delta float64) bool {
	return float64(mathutil.Abs(left-right)) <= delta
}
